/*
 * @Author: guoyangf
 * @Date: 2020-07-09 15:27:07
 * @LastEditTime: 2020-08-04 10:26:24
 * @Description: file content
 */

import React, { Component } from 'react';
import { base, toast } from 'nc-lightapp-front'
const { NCModal, NCDiv, NCButton, NCTextArea } = base;
import { requestPromise } from 'src/erm/sscrppublic/common/utils/asyncFunctions.js';

class ReasonModalCtrl extends Component {

    constructor(props) {
        super(props);
        this.state = {
            multiLang: {},
            isShow: false
        }

        this.multiLangPromise = window.__SSC_MultiLangPromise;
        this.multiLangPromise.then((multiLang) => {
            this.setState({
                multiLang
            })
        })
    }

    componentDidUpdate(prevProps) {

    }

    show = () => {
        this.setState({
            isShow: true
        })
    }

    cancel = () => {
        this.setState({ isShow: false });
        this.props.setHandleReason('');
    }

    save = async () => {
        let multiLang = await this.multiLangPromise;
        let grid = this.props.reqParam;
        try {
            const result = await requestPromise({
                url: this.props.saveUrl,
                data: {
                    ...grid, userjson: JSON.stringify({
                        actiontype: this.props.actionType,
                        memo: this.props.handleReason,
                        tabcode: this.props.tabCode,
                        ...this.props.reasonModalUserJsonData && this.props.reasonModalUserJsonData
                    })
                }
            });

            this.setState({
                isShow: false
            })
            this.props.setHandleReason('');
            if (result.success) {
                this.props.saveSuccessCallback && this.props.saveSuccessCallback(this.props.actionType, result);
                toast({ title: this.props.confirmBtnName + multiLang['job-common-cg'] });
            }
        } catch (e) {
            toast({ title: e.message, duration: 5, color: 'danger', })
        }
    }




    render() {
        return (
            <NCModal
                fieldid='cancel-hang-up'
                show={this.state.isShow}
                id="handon-dialog"
                onHide={() => { this.cancel() }}
            >
                <NCModal.Header closeButton={true}>
                    {/*取消挂起*/}
                    <NCModal.Title>{this.props.confirmBtnName}</NCModal.Title>
                </NCModal.Header>
                <NCModal.Body>
                    <div>
                        <div>
                            {/*请输入取消挂起原因*/}
                            <NCTextArea
                                fieldid="cancel-hang-up-reason"
                                placeholder={this.props.textareaPlaceholder}
                                showMax={true}
                                max={200}
                                value={this.props.handleReason}
                                onChange={(val) => { this.props.setHandleReason(val) }}
                            />
                        </div>
                    </div>
                </NCModal.Body>
                <NCModal.Footer>
                    <NCButton fieldid="cancel-hang-up" colors="primary" onClick={this.save} >{this.props.confirmBtnName}</NCButton>
                    <NCButton fieldid="cancel" onClick={this.cancel} >{this.state.multiLang['job-common-qx']}</NCButton> {/* 取消 */}
                </NCModal.Footer>
            </NCModal>
        )
    }
}

export default ReasonModalCtrl;